package com.ocom.company.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.company.EntityComDockApi;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.request.company.GetApiListRequest;
import com.ocom.common.vo.company.ComDockApiListVo;
import com.ocom.common.vo.company.GetDockApiConfigVo;
import com.ocom.common.vo.company.GetFeatureVersionVo;
import com.ocom.company.dao.IDaoComDockApi;
import com.ocom.company.service.ComDockApiService;
import com.ocom.mysql.aspect.DataSourceAspect;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ComDockApiServiceImpl extends ServiceImpl<IDaoComDockApi, EntityComDockApi> implements ComDockApiService {

	@Autowired
	IDaoComDockApi iDaoComDockApi;

	@Override
	@DataSourceAspect(type = DataSourceEnum.MAIN_DB)
	public EntityComDockApi getComDockApi(String dockCode, Long comId, String dockApiCode, Integer apiPermission) {
		return iDaoComDockApi.getComDockApi(dockCode,comId,dockApiCode,apiPermission);
	}

	@Override
	@DataSourceAspect(type = DataSourceEnum.MAIN_DB)
	public List<ComDockApiListVo> getComDockApiList(Long comId) {
		return iDaoComDockApi.getComDockApiList(comId);
	}

	@Override
	@DataSourceAspect(type = DataSourceEnum.MAIN_DB)
	public boolean removeComDockApiByComIdAndDockCodeAndGrade(Long comId, String dockCode) {
		QueryWrapper<EntityComDockApi> queryWrapper = Wrappers.query();
		queryWrapper.eq("com_id", comId);
		queryWrapper.eq("dock_code", dockCode);
		return this.remove(queryWrapper);
	}

	@Override
	@DataSourceAspect(type = DataSourceEnum.MAIN_DB)
	public EntityComDockApi getComDockApiInfo(String dockCode, Long comId, String dockApiCode, Integer apiPermission) {
		QueryWrapper<EntityComDockApi> queryWrapper = Wrappers.query();
		if (StringUtils.isNotBlank(dockCode)){
			queryWrapper.eq("dock_code", dockCode);
		}
		if (null != comId){
			queryWrapper.eq("com_id", comId);
		}
		if (StringUtils.isNotBlank(dockApiCode)){
			queryWrapper.eq("dock_api_code", dockApiCode);
		}
		if (null != apiPermission){
			queryWrapper.eq("api_permission", apiPermission);
		}
		queryWrapper.last("limit 1");
		return this.getOne(queryWrapper);
	}

	@Override
	@DataSourceAspect(type = DataSourceEnum.MAIN_DB)
	public List<GetDockApiConfigVo> getDockApiConfig(Long comId, String dockCode) {
		return iDaoComDockApi.getDockApiConfig(comId, dockCode);
	}

	@Override
	@DataSourceAspect(type = DataSourceEnum.MAIN_DB)
	public Boolean saveDockApiConfig(EntityComDockApi entityComDockApi) {
		EntityComDockApi entity = getComDockApiInfo(entityComDockApi.getDockCode(),entityComDockApi.getComId(),entityComDockApi.getDockApiCode(),null);
		if (null != entity) {
			entity.setApiPermission(entityComDockApi.getApiPermission());
			entity.setAutoCycle(entityComDockApi.getAutoCycle());
			entity.setAutoSize(entityComDockApi.getAutoSize());
		} else {
			entity = entityComDockApi;
		}
		return this.saveOrUpdate(entity);
	}

	@Override
	@DataSourceAspect(type = DataSourceEnum.MAIN_DB)
	public EntityComDockApi getApiPara(Long comId, String dockApiCode) {
		QueryWrapper<EntityComDockApi> queryWrapper = Wrappers.query();
		queryWrapper.eq("com_id", comId);
		queryWrapper.eq("dock_api_code", dockApiCode);
		queryWrapper.last("LIMIT 1");
		return this.getOne(queryWrapper);
	}

	@Override
	@DataSourceAspect(type = DataSourceEnum.MAIN_DB)
	public Boolean setApiPara(Long comId, String dockApiCode, String apiPara) {
		UpdateWrapper<EntityComDockApi> updateWrapper = Wrappers.update();
		updateWrapper.set("api_para", apiPara);
		updateWrapper.eq("com_id", comId);
		updateWrapper.eq("dock_api_code", dockApiCode);
		return this.update(updateWrapper);
	}

	@Override
	@DataSourceAspect(type = DataSourceEnum.MAIN_DB, isMaster = MasterSlaveEnum.SLAVE)
	public List<EntityComDockApi> getApiList(GetApiListRequest request) {
		return iDaoComDockApi.getApiList(request.getComId(), request.getDockCode(), request.getAutoCycle(), request.getDockApiCode());
	}

	@Override
	@DataSourceAspect(type= DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.MASTER)
	public List<GetFeatureVersionVo> getFeatureVersion(Long companyId) {
		return iDaoComDockApi.getFeatureVersion(companyId);
	}
}
